﻿#region

using System.Collections.Generic;
using System.Linq;
using Lab.DataContext.DataContext;
using Lab.Server.Domain;

#endregion

namespace Lab.Server.Infrastructure {
    public class VacancyRepository : Repository<Vacancy, vacancy> {
        public VacancyRepository(IDataContext dataContext) : base(dataContext) {}

        protected override vacancy TranslateToDataObject(Vacancy domainObject) {
            var vacancy = new vacancy {
                                          vacancy_nm = domainObject.Name,
                                          open_vacancy_dtm = domainObject.OpenVacancy,
                                          location_txt = domainObject.Location,
                                          expected_lvl_of_salary_amt = domainObject.ExpectedLevelOfSalary,
                                          is_open = domainObject.IsOpen
                                      };

            foreach (var skill in domainObject.Skills) {
                vacancy.skills_for_vacancies.Add(new SkillRepository(DataContext).GetSkillForVacancy(skill));
            }

            vacancy.kind_of_activity = new KindOfActivityRepository(DataContext)
                .GetKindOfActivityDataObject(domainObject.KindOfActivity);

            vacancy.employer = new EmployerRepository(DataContext).GetEmployerDataObject(domainObject.Employer);

            return vacancy;
        }

        public vacancy GetVacancyDataObject(Vacancy vacancy) {
            return DataContext.GetVacancies().Single(_ => _.vacancy_id == vacancy.Id);
        }

        public List<Vacancy> GetOpenVacancies() {
            return DataContext.GetVacancies()
                              .Where(_ => _.is_open)
                              .ToList()
                              .ConvertAll(ToDomainObject);
        }

        public Vacancy ToDomainObject(vacancy dataObject) {
            var domainObject = new Vacancy {
                                               Id = dataObject.vacancy_id,
                                               Name = dataObject.vacancy_nm,
                                               ExpectedLevelOfSalary = dataObject.expected_lvl_of_salary_amt,
                                               Location = dataObject.location_txt,
                                               OpenVacancy = dataObject.open_vacancy_dtm,
                                               IsOpen = dataObject.is_open,
                                               Employer = new EmployerRepository(DataContext)
                                                   .ToDomainObject(dataObject.employer),
                                               KindOfActivity = new KindOfActivityRepository(DataContext)
                                                   .TranslateToDomain(dataObject.kind_of_activity)
                                           };

            var skills = dataObject.skills_for_vacancies
                                   .Select(skillsForVacancy =>
                                           new SkillRepository(DataContext).TranslateToDomain(skillsForVacancy.skill))
                                   .ToList();

            domainObject.Skills = skills;

            return domainObject;
        }

        public List<Vacancy> GetAllVacancies() {
            return DataContext.GetVacancies().ConvertAll(ToDomainObject);
        }
    }
}